package jvalhondo.android.CuestaDelSol.app;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class CuestaDelSol extends ListActivity {
    private static final int ACTIVITY_CREATE = 0;
    private static final int ACTIVITY_EDIT = 1;
                
    private SMSDbAdapter mDbHelper;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms_list);
        
        mDbHelper = new SMSDbAdapter(this);
        mDbHelper.open();
        
        fillData();
        
        registerForContextMenu(getListView());

    } // close onCreate method

    private void fillData() {
        // Get all of the rows from the database and create the item list
        Cursor smsCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(smsCursor);

        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{SMSDbAdapter.KEY_TITLE};
        
        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.text1};

        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter sms = 
            new SimpleCursorAdapter(this, R.layout.sms_row, smsCursor, from, to);
        setListAdapter(sms);
    }

    // generate the bottom main menu
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }
    
    // main menu: bottom menu with add, help, about options
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            // Add Loan button clicked
            case R.id.add_newsms:
                Intent editIntent = new Intent(this, SMSEdit.class);
                startActivityForResult(editIntent, ACTIVITY_CREATE);
                return true;
            // About button clicked   
            case R.id.about:
                Intent aboutIntent = new Intent(this, AboutActivity.class);
                startActivity(aboutIntent);
                return true;
                
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    // generate the context menu when there is a long click on an item of the ListView
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
        menu.setHeaderTitle(R.string.context_menu_title);
    }

    // context menu: edit or delete an element of the ListView
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        // for using the same id on the onClick method
        final long id = info.id;
        // Handle item selection
        switch(item.getItemId()) {
            // Edit option clicked
            case R.id.edit:
                Intent editIntent = new Intent(this, SMSEdit.class);
                editIntent.putExtra(SMSDbAdapter.KEY_ROWID, info.id);
                startActivityForResult(editIntent, ACTIVITY_EDIT);
                return true;
             // Edit option clicked
            case R.id.sendSMS:
                try {
                                     Intent sendSMSIntent =  new Intent(Intent.ACTION_VIEW);
                                     Cursor sendSMSCursor = mDbHelper.fetchNote(id);
                                     startManagingCursor(sendSMSCursor);
                                     sendSMSIntent.putExtra("address","648053449");
                                     sendSMSIntent.putExtra("sms_body", sendSMSCursor.getString(
                                                          sendSMSCursor.getColumnIndexOrThrow(SMSDbAdapter.KEY_DESCRIPTION))); 
                                     sendSMSIntent.setType("vnd.android-dir/mms-sms");
                                     startActivity(sendSMSIntent);

                                } catch (Exception e) {
                                        Toast.makeText(getApplicationContext(),
                                                "Fallo en el envio, intentelo de nuevo",
                                                Toast.LENGTH_LONG).show();
                                        e.printStackTrace();
                                }
                return true;
            // Delete option clicked
            case R.id.delete:
                AlertDialog.Builder alertDialogDelete = new AlertDialog.Builder(CuestaDelSol.this);
                alertDialogDelete.setMessage(getResources().getString(R.string.delete_confirm_question));
                alertDialogDelete.setNegativeButton(getResources().getString(R.string.alert_dialog_cancel), null);
                alertDialogDelete.setPositiveButton(getResources().getString(R.string.alert_dialog_confirm), new AlertDialog.OnClickListener() {

                                // Delete confirm button
                                public void onClick(DialogInterface dialog, int which) {
                                        mDbHelper.deleteSMS(id);
                                        fillData();
                                }});
                alertDialogDelete.show();                       
                        return true;
            
            default:
                return super.onContextItemSelected(item);
        }
    }

    // editing/ updating when an element of the ListWiew is clicked
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent editIntent = new Intent(this, SMSEdit.class);
        editIntent.putExtra(SMSDbAdapter.KEY_ROWID, id);
        startActivityForResult(editIntent, ACTIVITY_EDIT);
    }

    // we get the result of startActivityForResult through this method
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();
    }
}